package cn.base.web.support.configure;

import cn.base.web.support.interceptor.AccessControlInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@Slf4j
public class DefaultWebMvcConfig implements WebMvcConfigurer{

	
	//@Autowired(required = false)
	//private AccessControlInterceptor accessControlInterceptor;
    @Autowired
    private AccessControlInterceptor accessControlInterceptor;

	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
		//允许跨域访问
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")// 设置允许跨域请求的域名
                .allowedHeaders("*")// 设置允许的请求头
                .allowCredentials(true)// 是否允许证书
                .allowedMethods("*")// 允许的方法
                .maxAge(3600);// 跨域允许时间单位秒
    }
    @Override
    public  void addResourceHandlers(ResourceHandlerRegistry registry) {
        //add-mappings 设置为false后,配置不生效,需要手动配置
        //不能配置为/** 因为配置为**后资源会匹配到ResourceHttpRequestHandler而不会抛出NoHandlerFoundException
        //Locations 结尾需要加 /
        registry.addResourceHandler("/html/**").addResourceLocations("classpath:/webapp/html/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/webapp/static/");
        //"file:/D:/xxx"
        //registry.addResourceHandler("/view/**").addResourceLocations("classpath:/webapp/view/");
        //registry.addResourceHandler("/static/**").addResourceLocations("classpath:/webapp/static/");
    }
	
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("Loaded AccessControlWebMvcConfig");
        registry.addInterceptor(accessControlInterceptor);

        //registry.addInterceptor(accessControlInterceptor);
                //.excludePathPatterns("/view/**","/static/**","/favicon.ico","/error");
		//if(antiReptileInterceptor!=null) {
		//	registry.addInterceptor(antiReptileInterceptor).excludePathPatterns("/view/**","/static/**","/favicon.ico","/error");
		//}
    }
}
	
	
